<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf8">
    <!--

    @license twgl.js Copyright (c) 2015, Gregg Tavares All Rights Reserved.
    Available via the MIT license.
    see: http://github.com/greggman/twgl.js for details

    -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <meta property="og:title" content="TWGL.js - text" />
    <meta property="og:type" content="website" />
    <meta property="og:image" content="http://twgljs.org/examples/screenshots/text.png" />
    <meta property="og:description" content="TWGL.js - text" />
    <meta property="og:url" content="http://twgljs.org" />

    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:site" content="@greggman">
    <meta name="twitter:creator" content="@greggman">
    <meta name="twitter:domain" content="twgljs.org">
    <meta name="twitter:title" content="TWGL.js - text">
    <meta name="twitter:url" content="http://twgljs.org/examples/text.html">
    <meta name="twitter:description" content="TWGL.js - text">
    <meta name="twitter:image:src" content="http://twgljs.org/examples/screenshots/text.png">

    <link href="/resources/images/twgljs-icon.png" rel="shortcut icon" type="image/png">

    <title>twgl.js - text</title>
    <style>
      body {
          margin: 0;
          font-family: monospace;
      }
      canvas {
          display: block;
          width: 100vw;
          height: 100vh;
      }
      #b {
        position: absolute;
        top: 10px;
        width: 100%;
        text-align: center;
        z-index: 2;
      }
    </style>
  </head>
  <body>
    <canvas id="c"></canvas>
    <div id="b"><a href="http://twgljs.org">twgl.js</a> - text</div>
  </body>
  <script id="vs" type="notjs">
uniform mat4 u_worldViewProjection;

attribute vec4 a_position;
attribute vec2 a_texcoord;

varying vec2 v_texcoord;

void main() {
  v_texcoord = a_texcoord;
  gl_Position = u_worldViewProjection * a_position;
}
  </script>
  <script id="fs" type="notjs">
precision mediump float;

varying vec2 v_texcoord;
uniform sampler2D u_texture;
uniform vec4 u_color;

void main() {
  vec4 color = texture2D(u_texture, v_texcoord) * u_color;
  if (color.a < 0.1) {
    discard;
  }
  gl_FragColor = color;
}
  </script>
  <script src="../3rdparty/chroma.min.js"></script>
  <script type="module">
    import * as twgl from '../dist/6.x/twgl-full.module.js';
    twgl.setDefaults({attribPrefix: "a_"});
    const m4 = twgl.m4;
    const gl = document.querySelector("#c").getContext("webgl");
    const programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);

    const bufferInfo = twgl.primitives.createPlaneBufferInfo(gl, 1, 1, 1, 1, m4.rotationX(Math.PI * 0.5));

    function rand(min, max) {
      return min + Math.random() * (max - min);
    }

    // Shared values
    const camera = m4.identity();
    const view = m4.identity();
    const viewProjection = m4.identity();
    const world = m4.identity();

    const ctx = document.createElement("canvas").getContext("2d");
    function makeText(text) {
      ctx.font = "20px monospace";
      const t = ctx.measureText(text);
      ctx.canvas.width  = Math.ceil(t.width) + 2;
      ctx.canvas.height = 24;
      ctx.font = "20px monospace";
      ctx.fillStyle = "white";
      ctx.textAlign = "center";
      ctx.textBaseAlign = "middle";
      ctx.fillText(text, ctx.canvas.width / 2 | 0, ctx.canvas.height / 2 | 0);
      return ctx.canvas;
    }

    const msgs = [
      "al",
      "joe",
      "jill",
      "brian",
      "thomas",
      "chrissy",
      "jennifer",
      "alexander",
    ];

    gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
    const textTextures = msgs.map(function(msg) {
      const canvas = makeText(msg);
      const scale = 0.01;
      return {
        tex: twgl.createTexture(gl, { src: canvas }),
        scale: [canvas.width * scale, canvas.height * scale, 1],
      };
    });

    const objects = [];
    const drawObjects = [];
    const numObjects = 500;
    const baseHue = rand(0, 360);
    for (let ii = 0; ii < numObjects; ++ii) {
      const texNdx = rand(0, textTextures.length) | 0;
      const text   = textTextures[texNdx];
      const uniforms = {
        u_texture: text.tex,
        u_worldViewProjection: m4.identity(),
        u_color: chroma.hsv((baseHue + rand(0, 60)) % 360, 1, 1).gl(),
      };
      drawObjects.push({
        programInfo: programInfo,
        bufferInfo: bufferInfo,
        uniforms: uniforms,
      });
      objects.push({
        translation: [rand(-10, 10), rand(-10, 10), rand(-10, 10)],
        ySpeed: rand(0.1, 0.3),
        zSpeed: rand(0.1, 0.3),
        uniforms: uniforms,
        texInfo: text,
      });
    }
    const clearColor = chroma.hsv((baseHue + 30) % 360, 0.5, 0.5).gl();

    function render(time) {
      time *= 0.001;
      twgl.resizeCanvasToDisplaySize(gl.canvas);
      gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

      gl.enable(gl.DEPTH_TEST);
      gl.enable(gl.BLEND);
      gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
      gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
      gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

      const projection = m4.perspective(30 * Math.PI / 180, gl.canvas.clientWidth / gl.canvas.clientHeight, 0.5, 100);
      const eye = [1, 4, -20];
      const target = [0, 0, 0];
      const up = [0, 1, 0];

      m4.lookAt(eye, target, up, camera);
      m4.inverse(camera, view);
      m4.multiply(projection, view, viewProjection);

      objects.forEach(function(obj) {
        const uni = obj.uniforms;
        const texInfo = obj.texInfo;
        m4.identity(world);
        m4.rotateY(world, time * obj.ySpeed, world);
        m4.rotateZ(world, time * obj.zSpeed, world);
        m4.translate(world, obj.translation, world);
        m4.rotateX(world, time, world);
        m4.scale(world, texInfo.scale, world);
        m4.multiply(view, world, world);

        m4.multiply(projection, world, uni.u_worldViewProjection);
      });

      twgl.drawObjectList(gl, drawObjects);

      requestAnimationFrame(render);
    }
    requestAnimationFrame(render);

  </script>
</html>




